﻿package com.msjt.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.msjt.base.BaseAction;
import com.msjt.entity.Question;
import com.msjt.service.IQuestionService;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;

/**
 * name:QuestionAction function:咨询模块的Action author: kw time:2013.8.27
 * updateTime:
 */
@SuppressWarnings("serial")
public class QuestionAction extends BaseAction{
	private Question question;
	@Resource
	private IQuestionService questionService;
	//page Model
	private PageMode page;
	private String message;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	@JSON(serialize=false)
	public PageMode getPage() {
		return page;
	}
	public void setPage(PageMode page) {
		this.page = page;
	}
	@JSON(serialize=false)
	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}
	@JSON(serialize=false)
	public IQuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}

	// 后台分页得到行业咨询
	public String seePage() throws Exception{
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("question_seePage.html");
		this.page.setModelDrive("page");
		//后台分页
		List<Question> questionList=this.questionService.getQuestionPageAfter(this.page.getPageIndex(), this.page.getPageSize());
		// 获取总页数
		 int total = this.questionService.getQuestionCount(null);
		// 置入总页数
		 page.setSumRows(total);
		// 创建分页链接
		page.createPageStr();
		// 将结果以及分页的链接放入请求中
		 this.getRequest().put("questionList", questionList);
		 this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
		return "showQuestion";
	}
	// 前台分页只加载ID，标题，问题内容，日期
	public String seeMore() throws Exception{
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("question_seeMore.html");
		this.page.setModelDrive("page");
		//后台分页
		List<Question> questionList=this.questionService.getQuestionSeeMore(this.page.getPageIndex(), this.page.getPageSize());
		// 获取总页数
		 int total = this.questionService.getQuestionCount(null);
		// 置入总页数
		 this.page.setSumRows(total);
		// 创建分页链接
		 this.page.createPageStr();
		// 将结果以及分页的链接放入请求中
		 this.getRequest().put("questionList", questionList);
		 this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
		return "showMore";
	}
	//检查验证码
	public String checkRand() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		String codeValue=request.getParameter("codeValue").toString();
		HttpSession sesseion=request.getSession();
		String rand=sesseion.getAttribute("rand").toString();	
		//PrintWriter out=this.getResponse().getWriter();
		//this.getResponse().setContentType("application/json;charset=utf-8");
		//this.getResponse().setCharacterEncoding("utf-8");
		if (rand.equals(codeValue)) {
			//String message="验证码正确";
			//out.print("验证码正确");
			this.message="验证码正确";
		}else{
			//out.print("验证码错误");
			this.message="验证码错误";
		}
		//out.flush();
		//out.close();
		return "input";
	}
	// 添加行业咨询
	public String addQuestion() throws Exception{
		//System.out.println(this.question.getQuesContent());
		this.question.setQuesType("通过");
		this.question.setQuesDate(new MyDate().getDateString());
		//this.question.setQuesAnswer("");
		boolean flag=this.questionService.addQuestion(this.question);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('提交咨询失败！');history.go(-1);</script>");
			return "error";
		}
		return "seeMore";
	}
	//准备回答，加载阶段
	public String preAnswer() throws Exception{
		Question q=this.questionService.getOneQuestion(this.question);
		this.getRequest().put("question", q);
		return "answer";
	}
	// 回答前需要查询出相应的咨询信息，回答咨询，回答完咨询后即变为关闭状态
	public String answerQuestion() throws Exception{
		boolean flag=this.questionService.answerQuestion(this.question);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('提交咨询失败！');history.go(-1);</script>");
			return "error";
		}
		return this.seePage();
	}
	//后台查看回答的详情
	public String showDetail() throws Exception{
		Question q=this.questionService.getOneQuestion(this.question);
		this.getRequest().put("question", q);
		return "detail";
	}
	// 管理咨询（删除）
	public String deleteQuestion() throws Exception{
		boolean flag=this.questionService.deleteQuestion(this.question);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('提交咨询失败！');history.go(-1);</script>");
			return "error";
		}
		return this.seePage();
	}
	// 审核咨询信息
	public String updateState() throws Exception{
		this.question.setQuesType("关闭");
		boolean flag=this.questionService.updateState(this.question);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('提交咨询失败！');history.go(-1);</script>");
			return "error";
		}
		return this.seePage();
		
	}
	//根据ID得到一个咨询
	public String seeDetail() throws Exception{
		Question q=this.questionService.getOneQuestion(this.question);
		this.getRequest().put("question", q);
		return "showDetail";
	}
	//修改回答前需要得到回答信息
	public String preUpAnswer() throws Exception{
		Question q=this.questionService.getOneQuestion(this.question);
		this.getRequest().put("question", q);
		return "updateAnswer";
	}
	//修改回答
	public String updateAnswer() throws Exception{
		boolean flag=this.questionService.updateAnswer(this.question);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('修改回答失败！');history.go(-1);</script>");
			return "error";
		}
		return this.showDetail();
	}
	//模糊查询
	public String select() throws Exception{
		if(this.question!=null){
			if(this.question.getQuesTitle().equals("")&&this.question.getQuesType().equals("")){
				this.question=null;
			}
		}
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("select.action");
		this.page.setModelDrive("page");
		//后台分页
		List<Question> questionList=this.questionService.selectByLike(this.question, this.page.getPageIndex(),this.page.getPageSize());
		// 获取总页数
		 int total = this.questionService.getQuestionCount(this.question);
		// 置入总页数
		 page.setSumRows(total);
		// 创建分页链接
		page.createPageStr();
		// 将结果以及分页的链接放入请求中
		 this.getRequest().put("questionList", questionList);
		 this.getRequest().put("link", page.getPagingStr());
		return "showQuestion";
	}
	//批量删除
		public String deleteMore() throws Exception{
			String idString=ServletActionContext.getRequest().getParameter("idString");
			idString=idString.substring(0,idString.length()-1);
			boolean flag=this.questionService.deleteMore(idString);
			if (!flag) {
				this.getRequest().put("msg",
						"<script>alert('删除失败！');history.go(-1);</script>");
				return "error";
			}
			return seePage();
		}
	
}
